package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.ContractDao;
import com.ubs.training.server.dao.model.Contract;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 3:22 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamContractDaoImpl implements ContractDao {

    DataSource ds;

    NamedParameterJdbcOperations ops;


    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }

    @Override
    public List<Contract> getAll() {
        return ops.query("select id_contract, id_product, id_client, start_date, max_borrow, status from contracts",
                Collections.EMPTY_MAP, new BeanPropertyRowMapper(Contract.class));
    }

    @Override
    public List<Contract> getByClientId(int clientId){
        return ops.query("select id_contract, id_product, id_client, start_date, max_borrow, status " +
                "from contracts where id_client=:clientId",
                Collections.singletonMap("clientId", clientId), new BeanPropertyRowMapper(Contract.class));
    }

    @Override
    public Contract getById(int idContract) {
        return ops.queryForObject("select id_contract, id_product, id_client, start_date, max_borrow, status " +
                "from contracts where id_contract=:contractId",
                Collections.singletonMap("contractId", idContract), new BeanPropertyRowMapper<Contract>(Contract.class));
    }

    @Override
    public void add(Contract contract) {
        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        contract.setIdContract(id);
        ops.update("insert into contracts values (:idContract, :idProduct, :idClient, :startDate, :maxBorrow, :status)",
                new BeanPropertySqlParameterSource(contract));
    }

    @Override
    public void update(Contract contract) {
        ops.update("update contracts set " +
                " id_product=:idProduct," +
                " id_client=:idClient," +
                " start_date=:startDate," +
                " max_borrow=:maxBorrow," +
                " status=:status " +
                "where id_contract=:idContract", new BeanPropertySqlParameterSource(contract));

    }

    @Override
    public void delete(Contract contract) {
        ops.update("delete from contracts where id_contract=:idContract", new BeanPropertySqlParameterSource(contract));
    }

    @Override
    public void delete(int id) {
        ops.update("delete from contracts where id_contract=:id", Collections.singletonMap("id", id));
    }
}
